package com.demo.mqtt.common.fromCommon.response;

import com.demo.mqtt.common.fromCommon.enums.CustomResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * <p>
 * 通用返回类，ok是正常返回
 * </p>
 *
 * @author gobigg
 * @since 2019/3/8
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 8992436576262574064L;

    private Integer code;
    private T data;
    private String message;
    private final long timestamps = System.currentTimeMillis();



    /**
     * 返回ok，code是200
     *
     * @param data 返回数据
     * @param <T>  数据类型
     * @return
     */
    public synchronized static <T> ResponseResult<T> ok(String message, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.OK.code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }

    /**
     * 返回ok，code是200，message是ok
     *
     * @param data 返回数据
     * @param <T>  数据类型
     * @return
     */
    public synchronized static <T> ResponseResult<T> ok(T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.OK.code);
        res.setMessage(ResponseCode.OK.msg);
        res.setData(data);
        return res;
    }

    /**
     * 返回ok，code是200，message是ok
     *
     * @param <T> 数据类型
     * @return
     */
    public synchronized static <T> ResponseResult<T> ok() {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.OK.code);
        res.setMessage(ResponseCode.OK.msg);
        return res;
    }


    /**
     * 返回异常。默认状态码500
     *
     * @param data 详细报错信息
     * @param <T>
     * @return
     */
    public synchronized static <T> ResponseResult<T> e(T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.INTERNAL_SERVER_ERROR.code);
        res.setMessage(ResponseCode.INTERNAL_SERVER_ERROR.msg);
        res.setData(data);
        return res;
    }

    /**
     * 返回异常。默认状态码500,自定义msg
     *
     * @param msg  自定义保存信息
     * @param data 详细报错信息
     * @param <T>
     * @return
     */
    public synchronized static <T> ResponseResult<T> e(String msg, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.INTERNAL_SERVER_ERROR.code);
        res.setMessage(msg);
        res.setData(data);
        return res;
    }

    /**
     * 返回异常。默认状态码500
     *
     * @param data
     * @param <T>
     * @return
     */
    public synchronized static <T> ResponseResult<T> e(Integer code, String msg, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(code);
        res.setMessage(msg);
        res.setData(data);
        return res;
    }

    public synchronized static <T> ResponseResult<T> sysOk (CustomResponseEnum responseEnum, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(responseEnum.getCode());
        res.setMessage((String) data);
        res.setData(data);
        return res;
    }
}
